package com.example.demo.service;


import com.example.demo.dao.*;
import com.example.demo.po.*;
import com.example.demo.util.AcutionCache;
import com.example.demo.util.DateUtil;
import com.example.demo.util.SeqFactory;
import com.example.demo.util.WxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TBizAuctionRecordService {

    @Autowired
    TBizAuctionRecordDAO tBizAuctionRecordDAO;
    @Autowired
    TBizOrderDAO tBizOrderDAO;
    @Autowired
    TBizOrderArtDAO tBizOrderArtDAO;
    @Autowired
    TBizAuctionArtDAO tBizAuctionArtDAO;
    @Autowired
    WxUserInfoDAO wxUserInfoDAO;
    @Autowired
    TBizArtDAO tBizArtDAO;
    @Autowired
    TBizAuctionDAO tBizAuctionDAO;
    @Autowired
    WxUserAddressDAO wxUserAddressDAO;

    /**
     * 某个拍卖会某个作品拍卖结束
     * @param ar
     * @return
     */
    @Transactional
    public  boolean saveAll(List<TBizAuctionRecord> ar){
        try{
            int i = tBizAuctionRecordDAO.insertList(ar);
            if(i>0){
                //获取最新的提条出价记录
                TBizAuctionRecord lastAt = ar.get(ar.size()-1);
                //生成拍卖会作品订单
                TBizOrder orderInfo = new TBizOrder();
                String orderId = "O" + SeqFactory.getNewSequenceAlone();
                orderInfo.setOrderid(orderId);
                orderInfo.setUserid(lastAt.getUserId());
                orderInfo.setCreatetime(DateUtil.formatDate(new Date()));
                //获取佣金
                TBizAuctionArt aa = new TBizAuctionArt();
                aa.setAuctionid(lastAt.getAuctionId());
                aa.setArtcode(lastAt.getArtCode());
                TBizAuctionArt taa = tBizAuctionArtDAO.findByAA(aa);
                //计算加上佣金的签
                Double commission=Double.valueOf(taa.getCommission())/100;
                System.out.println("佣金比例是 "+commission.toString());
                Double cprice =Double.valueOf(lastAt.getNewPrice())*(commission);
                System.out.println("佣金 "+cprice.toString());
                Double newPice = Double.valueOf(lastAt.getNewPrice())+cprice;
                orderInfo.setPrice(newPice.toString());
                orderInfo.setSjprice(lastAt.getNewPrice());

                TBizArt art = new TBizArt();
                art.setArtcode(lastAt.getArtCode());
                //判断是否是用户自己出价
                WxUserInfo wxUserInfo = wxUserInfoDAO.isAI(lastAt.getUserId());
                if(wxUserInfo!=null && (wxUserInfo.getOpenid()!=null && !"".equals(wxUserInfo.getOpenid().trim()))){
                    //AI出价 订单默认支付结束
                    orderInfo.setOrderstate("2");
                    orderInfo.setJzstate("2");
                    art.setSalestate("2");
                }else{
                    //获取用户默认的地址
                    WxUserAddress aInfo = wxUserAddressDAO.findByuserId(lastAt.getUserId());
                    if(aInfo!=null && aInfo.getId()!=null)
                        orderInfo.setAddress(aInfo.getAddress());
                    orderInfo.setOrderstate("1");
                    orderInfo.setJzstate("1");
                    art.setSalestate("1");
                }
                orderInfo.setType("2");
                orderInfo.setPaytype("2");
                tBizOrderDAO.save(orderInfo);
                //围观人数的统计
                if(new AcutionCache().anumberMap.containsKey(lastAt.getAuctionId())){
                    TBizAuction ac = new TBizAuction();
                    ac.setAuctionid(lastAt.getAuctionId());
                    ac.setSpace(new AcutionCache().getAnumberMap(lastAt.getAuctionId()).toString());
                    tBizAuctionDAO.uodaterByAid(ac);
                }

                TBizOrderArt oa = new TBizOrderArt();
                oa.setArtcode(lastAt.getArtCode());
                oa.setAcutionId(lastAt.getAuctionId());
                oa.setOrderid(orderId);
                oa.setNumber(1);
                tBizOrderArtDAO.save(oa);
                tBizArtDAO.updateByartCode(art);
                //发送下单成功消息
                if(wxUserInfo!=null && (wxUserInfo.getOpenid()!=null && !"".equals(wxUserInfo.getOpenid().trim()))){
                    Map<String, String> map = new HashMap<>();
                    map.put("key1","未支付");
                    map.put("key2",DateUtil.formatDate(new Date()));
                    map.put("key3",taa.getSpace());
                    map.put("key4","请尽快支付");
                    System.out.println(map.toString());
//                    WxUtil.sendType(lastAt.getUserId(),"3",map);
                }
                return true;
            }
            else
                return false;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 出价记录保存
     * @param list
     */
    public void recodeSave(List<TBizAuctionRecord>  list){
        tBizAuctionRecordDAO.insertList(list);
    }


    /**
     * 拍卖会结束
     * @param auctionid
     * @return
     */
    public boolean aucEnd(String auctionid){
        //根据拍卖会ID 获取所有作品
        List<TBizAuctionArt> list = tBizAuctionArtDAO.findByaId(auctionid);
        if(list!=null && list.size()>0){
            AcutionCache ac = new AcutionCache();
            for(TBizAuctionArt aa :list){
                String key = auctionid+"_"+aa.getArtcode();
                //获取出价记录
                List<TBizAuctionRecord> rList = ac.getCodeMap(key);
                if(rList!=null && rList.size()>0){
                    //该拍卖会的这个作品的出价记录没有保存到数据库
                    if(saveAll(rList)){
                        ac.remove(key,key);
                    }
                }
            }
            return true;
        }
        return false;
    }


}
